'use strict';

/**
 * Config for the router
 */
angular.module('app').config(['$stateProvider', '$urlRouterProvider', 'JQ_CONFIG', 'MODULE_CONFIG',function ($stateProvider,   $urlRouterProvider, JQ_CONFIG, MODULE_CONFIG) {
          
          


          $urlRouterProvider.otherwise('/access/signin');
          
          $stateProvider
              // others
              .state('lockme', {
                  url: '/lockme',
                  templateUrl: 'modular/system/tpl/page_lockme.html'
              })
              .state('access', {
                  url: '/access',
                  template: '<div ui-view class="fade-in-right-big smooth"></div>'
              })
              .state('access.signin', {
                  url: '/signin',
                  templateUrl: 'modular/system/tpl/page_signin.html',
                  resolve: load( ['modular/system/controllers/signinCtl.js'] )
              })
              .state('access.signup', {
                  url: '/signup',
                  templateUrl: 'modular/system/tpl/page_signup.html',
                  resolve: load( ['modular/system/controllers/signupCtl.js'] )
              })
              .state('access.forgotpwd', {
                  url: '/forgotpwd',
                  templateUrl: 'modular/system/tpl/page_forgotpwd.html'
              })
              .state('access.404', {
                  url: '/404',
                  templateUrl: 'modular/system/tpl/page_404.html'
              });
              
              
          $stateProvider.state('app', {
              abstract: true,
              url: '/app',
              templateUrl: "modular/system/tpl/app.html"
          })
          .state('app.dashboard', {
              url: '/dashboard',
              templateUrl: 'modular/system/tpl/app_dashboard.html',
              resolve: load(['modular/system/controllers/dashboardCtl.js'])
          });


          
          $stateProvider.state('app.dict', {
              abstract: true,
              url: '/dict',
              templateUrl: 'modular/system/tpl/dict/dict.group.html',
              resolve: load( ['angularBootstrapNavTree','modular/system/controllers/dictCtl.js'] )
          }).state('app.dict.list', {
              url: '/list/{id}',
              templateUrl: 'modular/system/tpl/dict/dict.list.html'
          }).state('app.dict.newgroup', {
              url: '/newgroup/{id}',
              templateUrl: 'modular/system/tpl/dict/dict.group.new.html'
          }).state('app.dict.new', {
              url: '/new',
              templateUrl: 'modular/system/tpl/dict/dict.new.html'
          })
          
          
          
          
          
          
          
          
          function load(srcs, callback){
        	  return {
        	        deps: ['$ocLazyLoad', '$q',
        	          function( $ocLazyLoad, $q ){
        	            var deferred = $q.defer();
        	            var promise  = false;
        	            srcs = angular.isArray(srcs) ? srcs : srcs.split(/\s+/);
        	            if(!promise){
        	              promise = deferred.promise;
        	            }
        	            angular.forEach(srcs, function(src) {
        	              promise = promise.then( function(){
        	                if(JQ_CONFIG[src]){
        	                  return $ocLazyLoad.load(JQ_CONFIG[src]);
        	                }
        	                angular.forEach(MODULE_CONFIG, function(module) {
        	                  if( module.name == src){
        	                    name = module.name;
        	                  }else{
        	                    name = src;
        	                  }
        	                });
        	                return $ocLazyLoad.load(name);
        	              } );
        	            });
        	            deferred.resolve();
        	            return callback ? promise.then(function(){ return callback(); }) : promise;
        	        }]
        	    }
          }
          
          
      }
    ]
  );
